﻿using System.Collections.Generic;
using System.Data;
using FacebookWrapper.ObjectModel;

namespace Desktop_Facebook
{
    public class FetchInfo
    {
        private const bool v_CheckedRow = true;

        public DataTable FriendInteractionStatisticsTable { get; set; }

        public DataTable EventsTable { get; set; }

        public DataTable BirthdaysTable { get; set; }

        public DataTable InboxTable { get; set; }

		public FriendScoreGenerator FriendScoreGenerator
		{
			get { return r_FriendScoreGenerator; }
		}

		public BirthdayHolder BirthdayHolder 
        { 
            get { return r_BirthdayHolder; } 
        }

		private readonly BirthdayHolder r_BirthdayHolder = new BirthdayHolder();

		private readonly FriendScoreGenerator r_FriendScoreGenerator = new FriendScoreGenerator();

        public void GetAll()
        {
            GenerateFriendInteractionStatistics();
            GetEvents();
            GetBirthdays();
            GetInbox();
        }

        public void GenerateFriendInteractionStatistics()
        {
            FriendInteractionStatisticsTable = new DataTable();
            
            FriendInteractionStatisticsTable.Columns.Add("Object");
            FriendInteractionStatisticsTable.Columns.Add("Name");
            FriendInteractionStatisticsTable.Columns.Add("Points");
			FriendInteractionStatisticsTable.Columns[0].DataType = System.Type.GetType("System.Object");
			FriendInteractionStatisticsTable.Columns[2].DataType = System.Type.GetType("System.Int32");

            FriendScoreGenerator.GenerateScores();

            foreach (User currentFriend in Session.Instance.LoggedInUser.Friends)
            {
                FriendInteractionStatisticsTable.Rows.Add(currentFriend, currentFriend.Name, FriendScoreGenerator.Scores[currentFriend.Id].Score);
            }
        }

        public void GetEvents()
        {
            EventsTable = new DataTable();

            EventsTable.Columns.Add("Object");
            EventsTable.Columns.Add("Selected");
            EventsTable.Columns.Add("Date");
            EventsTable.Columns.Add("Event Name");
            EventsTable.Columns.Add("Owner");
            EventsTable.Columns.Add("Location");
			EventsTable.Columns[0].DataType = System.Type.GetType("System.Object");
			EventsTable.Columns[1].DataType = System.Type.GetType("System.Boolean");
			EventsTable.Columns[2].DataType = System.Type.GetType("System.DateTime");

            foreach (Event currentEvent in Session.Instance.LoggedInUser.Events)
            {
                EventsTable.Rows.Add(currentEvent, !v_CheckedRow, currentEvent.StartTime, currentEvent.Name, currentEvent.Owner.Name, currentEvent.Location);
            }
        }

        public void GetBirthdays()
        {
            BirthdaysTable = new DataTable();

            BirthdaysTable.Columns.Add("Object");
            BirthdaysTable.Columns.Add("Selected");
            BirthdaysTable.Columns.Add("Event Name");
            BirthdaysTable.Columns.Add("Date");
			BirthdaysTable.Columns[0].DataType = System.Type.GetType("System.Object");
			BirthdaysTable.Columns[1].DataType = System.Type.GetType("System.Boolean");

            r_BirthdayHolder.MakeBirthdays();

            foreach (User currentFriend in Session.Instance.LoggedInUser.Friends)
            {
                if (r_BirthdayHolder.Birthdays.ContainsKey(currentFriend.Id))
                {
                    BirthdaysTable.Rows.Add(currentFriend, !v_CheckedRow, currentFriend.Name, r_BirthdayHolder.Birthdays[currentFriend.Id]);
                }
            }
        }

        public void GetInbox()
        {
            List<Message> filter = new MessageFilterer().AllMessages();
            
            PopulateInboxDataTable(filter);
        }

        public void PopulateInboxDataTable(List<Message> i_Messages)
        {
            InboxTable = new DataTable();

            InboxTable.Columns.Add("Object");
            InboxTable.Columns.Add("Selected");
            InboxTable.Columns.Add("From");
            InboxTable.Columns.Add("Message");
            InboxTable.Columns.Add("Date");
			InboxTable.Columns[0].DataType = System.Type.GetType("System.Object");
			InboxTable.Columns[1].DataType = System.Type.GetType("System.Boolean");

            foreach (Message currentMessage in i_Messages)
            {
                if (currentMessage.From != null)
                {
                    InboxTable.Rows.Add(currentMessage, !v_CheckedRow, currentMessage.From.Name, currentMessage.Text, currentMessage.CreatedTime);
                }
            }
        }
    }
}
